package com.example.openguass.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.openguass.entity.Dish;
import com.example.openguass.mapper.DishMapper;
import com.example.openguass.service.DishFlavorService;
import com.example.openguass.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.openguass.vo.DishFlavorVO;
import com.example.openguass.vo.DishPageVO;
import com.example.openguass.vo.DishVO;
import com.example.openguass.vo.MenuDishVO;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    private DishMapper dishMapper;
    private DishFlavorService dishFlavorService;
    @Autowired
    private void setDishMapper(DishMapper dishMapper){
        this.dishMapper = dishMapper;
    }
    @Autowired
    private void setDishFlavorService(DishFlavorService dishFlavorService){
        this.dishFlavorService = dishFlavorService;
    }

    @Override
    public Boolean setNullByCategoryId(String categoryId) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category",categoryId);
        Dish dish = new Dish();
        dish.setCategory("null");
        dish.setUpdateDate(LocalDateTime.now());
        return dishMapper.update(dish,wrapper) > 0;
    }

    @Override
    public Boolean exit(DishVO dishVO) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("name",dishVO.getName());
        return dishMapper.selectCount(wrapper) > 0;
    }

    @Override
    public Boolean insert(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        dish.setCreateDate(LocalDateTime.now());
//        dish.setId(UUID.randomUUID().toString());
//        if (!dishFlavorService.insert(new DishFlavorVO(null,dish.getId(),dishVO.getDishFlavor()))){
//            return false;
//        }
        return dishMapper.insert(dish) > 0;
    }

    @Override
    public Boolean update(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        dish.setUpdateDate(LocalDateTime.now());
        return dishMapper.updateById(dish) > 0;
    }

    @Override
    public IPage<Dish> findPage(DishPageVO pageVO) {
        Page<Dish> page = new Page<>(pageVO.getPageNo(),pageVO.getPageSize());
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        //构造查询匹配器
        if (StringUtils.isNotBlank(pageVO.getName())){
            wrapper.eq("name",pageVO.getName());
        }
        if (StringUtils.isNotBlank(pageVO.getCategory())){
            wrapper.eq("category",pageVO.getCategory());
        }
        wrapper.orderByDesc("create_date");
        return dishMapper.selectPage(page,wrapper);
    }

    @Override
    public List<MenuDishVO> getByCategory(String categoryName) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category",categoryName);
        List<Dish> dishes = dishMapper.selectList(wrapper);
        List<MenuDishVO> menuDishList = new ArrayList<>();
        for (Dish dish : dishes){
            MenuDishVO menuDishVO = new MenuDishVO();
            menuDishVO.setDishid(dish.getId());
            menuDishVO.setName(dish.getName());
            menuDishVO.setChild(null);
            menuDishVO.setNumber(1);
            menuDishVO.setDishflavor(null);
            menuDishVO.setAmount(dish.getPrice());
            menuDishList.add(menuDishVO);
        }
        return menuDishList;
    }
}
